import pylab as plt
import os
import math
from params import *

hop_file = os.path.join(WORKDIR, 'gowalla', 'temporal', "results",'gowalla_hops.txt')

MAX_HOP = 15
nums = {}
dens = {}
geo_nums = {}
geo_dens = {}
scatter = {}

for line in open(hop_file):
    v = line.strip().split()
    n1,n2, k1, k2, hop_dist = map(int,v[:5])
    if hop_dist == -1:
        hop_dist = 0
    if hop_dist > MAX_HOP:
        continue
    circle1, circle2 = map(int,v[5:7])

    edge_dist = float(v[7])
    geo_circle1,geo_circle2 = map(int,v[8:])

    nums.setdefault(hop_dist,0)
    nums[hop_dist] += 2
    dens.setdefault(hop_dist,0)
    dens[hop_dist] += circle1+circle2

    geo_nums.setdefault(edge_dist,0)
    geo_nums[edge_dist] += 2
    geo_dens.setdefault(edge_dist,0)
    geo_dens[edge_dist] += geo_circle1+geo_circle2

    scatter.setdefault(edge_dist,{}).setdefault(hop_dist,0)
    scatter[edge_dist][hop_dist] += 1

values = {}
for dist in sorted(scatter):
    for hop in scatter[dist]:
        values.setdefault(hop,[]).append(scatter[dist][hop])

def geometric_avg(s):
    x = [math.log(i) for i in s]
    avg = sum(x)/len(x)
    return math.exp(avg)

def arithmetic_avg(s):
    return sum(s)/len(s)

y2 = []
y1 = []
for h  in sorted(values):
    a1 = geometric_avg(values[h])
    a2 = arithmetic_avg(values[h])
    y1.append(a1)
    y2.append(a2)

x = sorted(values)
plt.figure()
plt.clf()
plt.axes(FIG_AXES2)
#plt.semilogy(x[1:],y1[1:],'k-')
plt.semilogy(x,y1,'ko', ms=6)
plt.xlabel(r'$h$')
plt.ylabel(r'$D_h$ [km]')
plt.grid(True)
plt.savefig('hop_geo.pdf')
plt.close()

